clear all
set more off, permanently

capture log close

**************************** FIGURE 1 ****************************************

********* prepare PADDD data

import delimited "C:\Users\dkeles\Documents\these\article2\publication JAERE\replication\csv_data\PADDD.csv", delimiter(",") encoding(UTF-8) clear 
rename fidn52 nearY_PADDD
rename year_padddc254 year_PADDD
rename protecte_1c254 namePADDD 
drop near_fidn100 near_distn1911 geodatatypc254 regionc254 countryc254 iso_3166_1c254 wdpa_idc254 pa_coden1911 protectedc254  paddd_revec254 reversal_yc254 
rename padddtrackn100 PADDD
rename location_kc254 location_k
rename typec254 typePADDD
rename statusc254 statusPADDD
rename year_gazetc254 year_gazet
rename causec254 causePADDD
rename area_padddc254 areaPADDD
rename size_pre_pc254 sizeprePADDD
rename size_post_c254 sizepostPADDD
rename iucn_categc254 IUCNprePADDD
rename iucn_cat_1c254 IUCNpostPADDD 
rename offset_c254 offsetPADDD
rename systemic_cc254 sys_change

encode typePADDD, gen(type)
encode statusPADDD, gen(status)

bysort year_PADDD: egen enac_down=count(PADDD) if statusPADDD=="Enacted" & typePADDD=="downsize"
bysort year_PADDD: egen enac_degaz=count(PADDD) if statusPADDD=="Enacted" & typePADDD=="degazette"
bysort year_PADDD: egen enac_dwng=count(PADDD) if   statusPADDD=="Enacted" & typePADDD=="downgrade" 
bysort year_PADDD: egen pro_down=count(PADDD) if statusPADDD=="Proposed" & typePADDD=="downsize"
bysort year_PADDD: egen pro_degaz=count(PADDD) if statusPADDD=="Proposed" & typePADDD=="degazette"
bysort year_PADDD: egen pro_dwng=count(PADDD) if   statusPADDD=="Proposed" & typePADDD=="downgrade" 

 
foreach var in enac_down enac_degaz enac_dwng pro_down pro_degaz pro_dwng {
replace `var'=0 if `var'==.
}

rename year_PADDD year

count
set obs `=`r(N)'+1'
replace year=1972 if year==.
count
set obs `=`r(N)'+1'
replace year=1974 if year==.
count
set obs `=`r(N)'+1'
replace year=1975 if year==.
count
set obs `=`r(N)'+1'
replace year=1976 if year==.
count
set obs `=`r(N)'+1'
replace year=1977 if year==.
count
set obs `=`r(N)'+1'
replace year=1978 if year==.
count
set obs `=`r(N)'+1'
replace year=1979 if year==.
count
set obs `=`r(N)'+1'
replace year=1981 if year==.
count
set obs `=`r(N)'+1'
replace year=1982 if year==.
count
set obs `=`r(N)'+1'
replace year=1983 if year==.
count
set obs `=`r(N)'+1'
replace year=1984 if year==.
count
set obs `=`r(N)'+1'
replace year=1985 if year==.
count
set obs `=`r(N)'+1'
replace year=1986 if year==.
count
set obs `=`r(N)'+1'
replace year=1987 if year==.
count
set obs `=`r(N)'+1'
replace year=1989 if year==.
count
set obs `=`r(N)'+1'
replace year=1990 if year==.
count
set obs `=`r(N)'+1'
replace year=1992 if year==.
count
set obs `=`r(N)'+1'
replace year=1993 if year==.
count
set obs `=`r(N)'+1'
replace year=1994 if year==.
count
set obs `=`r(N)'+1'
replace year=1997 if year==.
count
set obs `=`r(N)'+1'
replace year=1998 if year==.


replace areaPADDD="." if areaPADDD=="unknown"
replace areaPADDD="." if areaPADDD=="unk"
replace areaPADDD="." if areaPADDD==""
destring areaPADDD, replace

bysort year: egen a_enac_down=sum(areaPADDD) if   statusPADDD=="Enacted" & typePADDD=="downsize"
bysort year: egen a_enac_degaz=sum(areaPADDD) if   statusPADDD=="Enacted" & typePADDD=="degazette"
bysort year: egen a_enac_dwng=sum(areaPADDD) if    statusPADDD=="Enacted" & typePADDD=="downgrade" 
bysort year: egen a_pro_down=sum(areaPADDD) if statusPADDD=="Proposed" & typePADDD=="downsize"
bysort year: egen a_pro_degaz=sum(areaPADDD) if  statusPADDD=="Proposed" & typePADDD=="degazette"
bysort year: egen a_pro_dwng=sum(areaPADDD) if   typePADDD=="downgrade" & statusPADDD=="Proposed" 

foreach var in enac_down enac_degaz enac_dwng pro_down pro_degaz pro_dwng {
replace `var'=0 if `var'==.
}
foreach var in a_enac_down a_enac_degaz a_enac_dwng a_pro_down a_pro_degaz a_pro_dwng {
replace `var'=0 if `var'==.
}
replace areaPADDD=0 if areaPADDD==.

save "C:\Users\dkeles\Documents\these\article2\publication JAERE\replication\PADDD_fig.dta", replace


********** prepare PA data

clear
use  "C:\Users\dkeles\Documents\these\article2\publication JAERE\replication\csv_data\PA.dta"

drop if marinePA==1
rename yr_desigPa year

save "C:\Users\dkeles\Documents\these\article2\publication JAERE\replication\PA_fig.dta", replace

 
********** draw figure

clear

use "C:\Users\dkeles\Documents\these\article2\publication JAERE\replication\PADDD_fig.dta"
 
merge m:m year using "C:\Users\dkeles\Documents\these\article2\publication JAERE\replication\PA_fig.dta"

bysort year: egen desigPa= count(IDwdpa) 

cumul year, gen(yr)
sort yr 
line yr year, ylab(, grid) ytitle("") xlab(, grid)

bysort year: gen n=_n
sort year n
replace desigPa=0 if n>1
gen cum_PA = sum(desigPa)

graph twoway (bar enac_down enac_degaz year , bcolor(gs10 gs5)  ) ///
             (line cum_PA year , ylab(, grid) ytitle("") xlab(, grid) yaxis(2) lcolor(black) ) ///
		      , xlabel(1960(2)2014, angle(90)) xtitle("Years") ///
		        ylabel(0(2)10, angle(horizontal)) ytitle("Yearly number of PA size reductions") ytitle("Cumulative number of PA designations", axis(2))  ///
		        graphregion(fcolor(white)) legend(size(vsmall) label(1 "Enacted Downsizings") label(2 "Enacted Degazettements") label(3 "Cumulative number of PA designations")) 
		
graph export  "...\figure1.png", replace


**************************** FIGURE 2 ****************************************

clear all

import excel "C:\Users\dkeles\Documents\these\article2\statdes\prodes2019.xlsx", sheet("Feuil1") 

drop E F G H I 
drop D J

rename A year
rename B def 
rename C cum_def


graph twoway (line def year, ytitle("Yearly Deforestation (km²)") lcolor(black) ) , ///
		     xtitle("Year") graphregion(fcolor(white)) xlab(2001(1)2020, labsize(small) angle(45)) legend(off)
		
graph export  "...\figure2.png", replace





